<%@ Page Trace="false" Language="C#" %>

<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>

<H2>Creating XML</H2>
<script runat=server>


	// Please note that defining classes in pages like this is bad practise in general since
	// in doesn't make them reusable ! :)  Since this is just a sample, I can get away with it

	class HtmlStream : Stream
	{
	
		bool CanRead()
		{
			get { return true; }
		}
	}
	
	protected void RenderSimpleXML()
	{
		MemoryStream ms;
		ms = new MemoryStream();
		
		XmlTextWriter tw;
		
		tw = new XmlTextWriter( new StreamWriter(ms) );
		tw.Formatting = Formatting.Indented;
		tw.Indentation = 4;
		tw.WriteStartDocument();
		tw.WriteComment("A Simple XML File");
		tw.WriteStartElement("Books");
			tw.WriteStartElement("Book");
				tw.WriteAttribute("title","Professional ASP.NET");
			tw.WriteEndElement();
		tw.WriteEndElement();
		tw.WriteEndDocument();
		
		
		// Important to call to ensure text writer writes out to stream
		
		tw.Flush();
		
		// Reset stream position ready for reading
				
		ms.Seek( 0, SeekOrigin.Begin );

		TextReader tr = new StreamReader( ms );

		// Write out the XML inside of a PRE element to preserve XML formatting
		
		Response.Write("<pre>");		
		Response.Write( HttpUtility.HtmlEncode( tr.ReadToEnd() ) );
		Response.Write("</pre>");		

	}

</script>

<%
	RenderSimpleXML();
%>

